package org.misalen.web.database.repository.mysql;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.misalen.web.database.dao.mysql.impl.BaseMySqlDaoImpl;
import org.misalen.web.database.model.mysql.sys.SysRole;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class SysRoleRepository extends BaseMySqlDaoImpl<SysRole> {

	@Transactional
	public List<String> findCodeByAdmin(String admindId) {
		String sql = "SELECT SysRole.CODE AS CODE FROM SysAdminRole "
				+ "LEFT JOIN SysRole ON SysRole.primaryKey = SysAdminRole.roleId " + "WHERE SysAdminRole.adminId = '"
				+ admindId + "'";
		Session session = openSession();
		Query query = session.createSQLQuery(sql);
		List<String> list2 = new ArrayList<String>();
		@SuppressWarnings("unchecked")
		List<Object> list = query.list();
		for (Object object : list) {
			list2.add(String.valueOf(object));
		}
		closeSession(session);
		return list2;
	}

	public List<SysRole> findByAvailable() {
		String hql = "from SysRole where usingState='available' order by seq";
		Session session = openSession();
		Query query = session.createQuery(hql);
		@SuppressWarnings("unchecked")
		List<SysRole> list = query.list();
		closeSession(session);
		return list;
	}

}